package com.solo.eggsolo.service.impl;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.solo.eggsolo.entity.Admin;
import com.solo.eggsolo.mapper.AdminMapper;
import com.solo.eggsolo.service.AdminService;
import com.solo.eggsolo.util.JwtUtil;

import cn.hutool.json.JSONException;

/**
 * 
 * @author Yu
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AdminServiceImpl extends BaseServiceImpl<Admin> implements AdminService {

	@Autowired
	AdminMapper adminMapper;
	
	@Override
	public String login(String name, String password) {

		Admin admin = adminMapper.selectAdminByAdminname(name);

		if (admin == null) {
			throw new JSONException("用户名不存在");
		}

		if (!admin.getPassword().equals(password)) {
			throw new JSONException("密码错误");
		}

		HashMap<String, Object> adminInfo = new HashMap<>();
		adminInfo.put("role", "用户");
		adminInfo.put("id", admin.getId());
		adminInfo.put("name", admin.getName());

		return JwtUtil.sign(name, adminInfo);
	}
	
	@Override
	public Admin adminInfo(String token) {
		String adminname = JwtUtil.getUserId(token);
		
		return adminMapper.selectAdminByAdminname(adminname);
	}
	
	@Override
	public boolean resetPassword(Integer id) {
		
		return adminMapper.resetPassword(id);
	}
}
